import React from 'react'
import { connect } from 'dva'
import { Card } from 'antd'

/**
 * LatestNetValueList 最新净值统计表
 */
const LatestNetValueList = ({ 
  data, 
}) => {
  return(
    <table>
      <thead>
        <tr>
          {/*<th>日期</th>
          <th>最新净值</th>
          <th>日涨幅</th>*/}
          <th>最新净值:</th>
          <th>{data.value||'-'}</th>
          <th>日涨幅:</th>
          <th>{(data.increase===0||data.increase)&&`${Math.signFigures(data.increase * 100)}%`||'-'}</th>
          <th>日期:</th>
          <th>{data.date||'-'}</th>
        </tr>
      </thead>
      <tbody>
        <tr>
          {/*<td>{data.date||'-'}</td>
          <td>{data.value||'-'}</td>
          <td>{(data.increase===0||data.increase)&&`${Math.signFigures(data.increase * 100)}%`||'-'}</td>*/}
        </tr>
      </tbody>
    </table>
  )
}

/**
 * LatestNetValue 最新净值
 * 引用组件
 * LatestNetValueList 最新净值统计表
 */
class LatestNetValue extends React.Component {
  state={
    data:this.props.data
  }

  componentDidMount() {
    this.fetchData(this.props)
  }
  componentWillReceiveProps(nextProps) {
    if((nextProps.lPortId!==this.props.lPortId)||(nextProps.cBenchCode!==this.props.cBenchCode)){
      this.fetchData(nextProps)
    }
    if(nextProps.latestNetValue!==this.props.latestNetValue){
      this.setState({
        data:nextProps.latestNetValue
      })
    }
  }
  fetchData=(props)=>{
    this.props.dispatch({
      type:'combinatorialAnalysis/getLatestNetValue',
      payload:props
    })
  }
  render() {
    const { data } = this.state
    
    return (
      <Card title='最新净值' className="latestNetValue">
        <LatestNetValueList data={data} />
      </Card>
    )
  }
}

LatestNetValue.defaultProps={
  lPortId:null,
  cBenchCode:null,
  data: {
    date: '04-28',
    value: 118.09,
    increase: 0.0578
  },
}

export default connect()(LatestNetValue)
